package thread;

/**
 * Created with IntelliJ IDEA.
 *
 * @ Title
 * @ Description:
 * @ Auther: 赵彦博
 * @ Date: 2022-12-02
 * @ Time: 17:17
 */
@SuppressWarnings({"all"})
public class Demo7 {
    private static final long count = 10_0000_0000;

    public static void serial() {

        // 记录程序执行时间
        long beg = System.currentTimeMillis();
        long a = 0;
        for (long i = 0; i < count; i++) {
            a++;
        }
        long b = 0;
        for (long i = 0; i < count; i++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("消耗时间："+(end-beg)+"ms");
    }

    public static void concurrency() throws InterruptedException {
        long beg = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            long a = 0;
            for (long i = 0; i < count; i++) {
                a++;
            }
        });
        t1.start();
        Thread t2 = new Thread(()->{
            long b = 0;
            for (long i = 0; i < count; i++) {
                b++;
            }
        });
        t2.start();
        // 此处不能直接记录结束时间
        // main 和 t1 t2 之间是并发执行的关系，此处t1和t2还没执行玩=完呢，这里就开始记录结束时间了，这里显示不准确的
        // 正确做法应该是让main线程等待t1和t2跑完了，再来记录结束时间
        // join 效果就是等待线程结束
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("消耗时间："+(end-beg)+"ms");
    }

    public static void main(String[] args) throws InterruptedException {
        //serial();
        concurrency();
    }
}
